<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {
	
	
	public $user_data=array();
	
	function __construct()
     {
	     parent::__construct();
	     
		//不登陆的不让访问
		if (!$this->session->userdata('uadmin_in')){
			
			redirect('admin/home');
			exit();
		}
		
		$this->user_data["uadmin_name"] = $this->session->userdata('uadmin_name');
		$this->user_data["uadmin_userid"] = $this->session->userdata('uadmin_userid');
		$this->user_data["uadmin_account"] = $this->session->userdata('uadmin_account');
		
		$this->load->model('Order_model');

     }
     //订单管理
	public function index()
	{
		$data=array();
		$data["title"]="order_manage";
		$data["bookdate1"]=date('Y-m-d');
		
     	$this->load->view("admin/order/order_manage",$data);
     
	}
	
	//查询订单
	public function queryorder()
	{
		$data=array();
		
		$this->load->model("Order_model");
		
		$data["title"]	="query Order";
		$data["OrderStatusdesc"]=$this->config->item("OrderStatus");
		$data["result"]=$this->Order_model->query_adminorder($_POST,$this->user_data);
		
		
		$this->load->view("admin/order/queryorder",$data);
		
	}
	
	//显示订单详细页面并操作
	public function operorder($sOrderno)
	{
		$data=array();
		
		$this->lang->load('info',"english");
		
		$bookdata= $this->Order_model->get_airOrder($sOrderno,$this->user_data);
		
		$this->load->model('Air_model');
		
		$flightdata=explode("^",$bookdata["order"]->Flightinfo1."^");
	   	$data['arairinfo']=$this->Air_model->getarrbookinfo($flightdata[0],$flightdata[1]);
	   	
	   	$data['sOrderno']=$sOrderno;
	   	$data['title']="Order No:".$sOrderno;
	   	$data['sFromcity']=$bookdata["order"]->fromportName;
	   	$data['sDestcity']=$bookdata["order"]->destportName;
	   	$data['sDepartDate']=date("Y-m-d",strtotime($bookdata["order"]->departdate));
		$data['sReturnDate']=date("Y-m-d",strtotime($bookdata["order"]->returndate));
		if($data['sReturnDate']=="1970-01-01")
	   	{
	   		$data['sReturnDate']="";
	   	}
	   	
		$data['orderstatus']=$bookdata["order"]->Status;
		$data['LinkMan']=$bookdata["order"]->Linkman;
	   	$data['Mobile1']=$bookdata["order"]->LinkMobile1;
	   	$data['Mobile2']=$bookdata["order"]->LinkMobile2;
	   	$data['Email']	=$bookdata["order"]->LinkEmail;
	   	$data['mainCurrency']=$bookdata["order"]->mainCurrency;
	   	$data['PNR']		=$bookdata["order"]->PNR;
	   	$data['TotalCost']	=$bookdata["order"]->TotalCost;
	   	$data['refundFee']	=$bookdata["order"]->backmoney;
	   	
	   	if($bookdata["order"]->interface_result!="")
	   	{
		   	$interface=json_decode($bookdata["order"]->interface_result);
		   	
		   	//未支付成功的
		   	
		   	if(substr($interface->payresult,0,2)=="OK")
		   	{
		   		$data['isHuitrip']=false;
		   	}else
		   	{
		   		$data['isHuitrip']=true;
		   	}
	   	
	   	}else
	   	{
	   		$data['isHuitrip']=false;
	   	}
	   	
	   	
	   	$data['cpsdata']=$bookdata["passlist"];
	   	
	   	$data['TotalPrice']=$bookdata["order"]->TotalPrice;
	   	$data['TotalCost']=$bookdata["order"]->TotalCost;
	   	
	   	$data['TotalPassenger']=$bookdata["order"]->TotalPassenger;
	   	
	   	$arOrderStatus=$this->config->item("OrderStatus");
	   	$data['OrderStatusDesc'] =$arOrderStatus[$bookdata["order"]->Status];
	   	$data['aridType'] =$this->config->item("idType");
		$data['arPassType']=$this->config->item("PassType");
		$data['arGender']=lang("Gender");
		
		$data['loglist']=$this->Order_model->getOrderlog($sOrderno,"");
		
		$this->load->view("admin/order/showorder",$data);
	}
	
	//订单处理
	public function dooperorder()
	{
		
		$result= $this->Order_model->update_orderstatus($_POST,$this->user_data);
		
		if($result)
		{
			$url="operorder/".$_POST["Orderno"];
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"oper success\");";
			echo "window.location='$url';";
			echo "</SCRIPT>";  
		}
	}
	
	//向汇游支付订单
	public function dohuitrippay()
	{
		$this->load->model("Huitrip_model");
		
		$sOrderno=$this->input->post("Orderno");
		
		$this->Huitrip_model->hy_payOrder($sOrderno);
		
		$url="operorder/".$_POST["Orderno"];
		echo "<SCRIPT LANGUAGE=\"JavaScript\">";
		echo "alert(\"oper success\");";
		echo "window.location='$url';";
		echo "</SCRIPT>";  
	}
	//保存票号及成本
	public function saveTicketNo()
	{
		
		$result= $this->Order_model->update_orderTicketNo($_POST,$this->user_data);
		
		if($result)
		{
			$url="operorder/".$_POST["Orderno"];
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"oper success\");";
			echo "window.location='$url';";
			echo "</SCRIPT>";  
		}
	}
	//检查是否有没有保存的票号
	public function checkTicketNo()
	{
		
		$sOrderno=$this->input->get("Orderno");
		$result= $this->Order_model->checkTicketNo($sOrderno);
		$msg=array("Result" => $result);
		
		echo json_encode($msg);
	}
	
	
	//写日志
	public function writelog()
	{
		$sOrderno=$this->input->post("Orderno");
		
		//记录日志
		$admindata=array(
			"AgentCode" => $this->user_data["uadmin_account"],
			"AgentName" => $this->user_data["uadmin_name"],
			"user_account" => "admin",
			"user_name" => "admin"
		);
		
		$this->Order_model->write_orderlog($sOrderno,"writelog",$this->input->post("operNote"),$admindata);
		
		$url="operorder/".$_POST["Orderno"];
		echo "<SCRIPT LANGUAGE=\"JavaScript\">";
		echo "alert(\"oper success\");";
		echo "window.location='$url';";
		echo "</SCRIPT>";  
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */

?>
